#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
queue<pair<int, int> > q;
const int N = 1001;
char a[N][N];
int w[N][N];
bool b[N][N];
int xa, ya, xb, yb;
int n, m;

int dx[4] = { -1, 0, 1, 0 }, dy[4] = { 0, 1, 0, -1 };

int bfs()
{
	memset(w, -1, sizeof(w));
	memset(b, 0, sizeof(b));
	b[xa][ya] = 1;
	q.push(make_pair(xa, ya));
	while (!q.empty())
	{
		int x, y;
		x = q.front().first, y = q.front().second;
		q.pop();
		for (int i = 0; i < 4; i++)
		{
			int nx = x + dx[i], ny = y + dy[i];
			if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && b[nx][ny] == false && a[nx][ny] == '.')
			{
				b[nx][ny] = true;
				w[nx][ny] = w[x][y] + 1;
				q.push(make_pair(nx, ny));
			}
		}
	}
	return w[xb][yb] >= 0 ? w[xb][yb] + 1 : -1;
}

int main()
{
	cin >> n >> m;
	cin >> xa >> ya >> xb >> yb;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++)
			cin >> a[i][j];
	int result = bfs();
	if (result >= 0)
		cout << result << endl;
	else
		cout << "-1" << endl;
	return 0;
}
